package com.anpy.bill.service;

import com.anpy.bill.mapper.AccountMapper;
import com.anpy.bill.model.Account;
import com.anpy.bill.model.PageResult;
import com.anpy.common.service.model.result.ApiResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
public class AccountService {
    private final AccountMapper accountMapper;
    private static final Logger log = LoggerFactory.getLogger(AccountService.class);

    public AccountService(AccountMapper accountMapper) {
        this.accountMapper = accountMapper;
    }

    public ApiResult<?> createAccount(Account account, int userId) {
        account.setUserId(userId);
        accountMapper.insert(account);
        return ApiResult.success();
//
//        String directoryPath = "/data/data/com.termux/files/home/storage/shared/personal"; // 你可以指定绝对路径或相对路径
//
//        // 创建File对象
//        File directory = new File(directoryPath);
//
//        // 检查目录是否存在
//        if (!directory.exists()) {
//            // 创建目录
//            boolean created = directory.mkdir(); // mkdir() 创建单个目录
//            // boolean created = directory.mkdirs(); // mkdirs() 创建多级目录
//
//            if (created) {
//                System.out.println("目录创建成功: " + directoryPath);
//            } else {
//                System.out.println("目录创建失败: " + directoryPath);
//            }
//        } else {
//            System.out.println("目录已存在: " + directoryPath);
//        }
//
//        String filePath = "/data/data/com.termux/files/home/storage/shared/personal/personal_account_data_"+userId+".json"; // 这个路径指向SD卡的共享目录
//        try {
//            Files.write(Paths.get(filePath), account.getPhone().getBytes());
//            return ApiResult.success();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ApiResult.failed("账户创建成功但文件存储失败");
//        }

    }

    public Account getAccount(Long id, int userId) {
        return accountMapper.selectByIdAndUserId(id, userId);
    }

    public ResponseEntity<?> updateAccount(Long id, Account account, int userId) {
        account.setId(id);
        account.setUserId(userId);
        int affected = accountMapper.updateByIdAndUserId(account);
        return affected > 0 ? 
            ResponseEntity.ok().build() : 
            ResponseEntity.notFound().build();
    }

    public ResponseEntity<?> deleteAccount(Long id, int userId) {
        int affected = accountMapper.deleteByIdAndUserId(id, userId);
        return affected > 0 ? 
            ResponseEntity.noContent().build() : 
            ResponseEntity.notFound().build();
    }

    public PageResult<Account> getAccountsByUserId(int userId, int page, int size) {
        int offset = page * size;
        List<Account> accounts = accountMapper.selectByUserIdWithPage(userId, offset, size);
        int total = accountMapper.countByUserId(userId);
        return new PageResult<>(page, size, total, accounts);
    }
}
